//
// Created by 67678 on 2023/3/13.
//

#ifndef WEBSERVER_EPOLL_H
#define WEBSERVER_EPOLL_H

#include <array>
#include <sys/epoll.h>

#include "nonCopyable.h"

namespace socket_epoll {
class Epoll : util::NonCopyable {
public:
    enum EVENT_TYPE : unsigned int {
        IN = EPOLLIN,
        OUT = EPOLLOUT,
        ERR = EPOLLERR,
        HUP = EPOLLHUP,
        ET = EPOLLET,
        ONE_SHOT = EPOLLONESHOT,
    };


    static constexpr int MAX_CONNECTION = 100000;

    static Epoll &getInstance() {
        static Epoll e;
        return e;
    }

    void init();

    void close();

    int add(int fd, int state);

    int mod(int fd, int state);

    int del(int fd);

    int wait(int timeout = -1);

    std::array<epoll_event, MAX_CONNECTION> &get_events() {
        return events_;
    }

private:
    Epoll();

    ~Epoll();

    std::array<epoll_event, MAX_CONNECTION> events_;
    int epfd_ = -1;
};
}
#endif //WEBSERVER_EPOLL_H
